using System.Globalization;
using System.IO;
using Microsoft.Practices.RecipeFramework;
using VSSDKAssist.Assets.Actions.Base;
using VSSDKAssist.Assets.Actions.General;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.Common.Actions
{
	/// <summary>
	/// Action that creates a snk file
	/// </summary>
	public class CreateSnkAction : ActionBase
	{
		string snkFileName = "Key.snk";

		/// <summary>
		/// Gets or sets the name of the SNK file.
		/// </summary>
		/// <value>The name of the SNK file.</value>
		[Input(Required = false)]
		public string SnkFileName
		{
			get { return snkFileName; }
			set { snkFileName = value; }
		}

		private EnvDTE.Project project;

		/// <summary>
		/// Gets or sets the project.
		/// </summary>
		/// <value>The project.</value>
		[Input(Required=true)]
		public EnvDTE.Project Project
		{
			get { return project; }
			set { project = value; }
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			using(ExecuteProcessAction executeProcessAction = new ExecuteProcessAction())
			{
				executeProcessAction.Site = this.Site;

				string snExecutableFile =
					Path.Combine(
						Path.Combine(CommonHelper.GetVSInstallPath(), CommonHelper.GetWindowsSDKBinPath()),
						Constants.SNTool);

				string snkFilePath =
					Path.Combine(
						Path.GetDirectoryName(this.project.Properties.Item("FullPath").Value.ToString()),
						snkFileName);

				executeProcessAction.Arguments = string.Format(CultureInfo.CurrentCulture, @"-k ""{0}""", snkFilePath);
				executeProcessAction.FileName = snExecutableFile;
				executeProcessAction.Execute();

				this.project.ProjectItems.AddFromFile(snkFilePath);
			}			
		}
	}
}